package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ChangeAccountServlet extends HttpServlet {
    private String username;
    private String password;
    private String firstname;
    private String lastname;
    private String email;
    private String phone;
    private String address1;
    private String address2;
    private String city;
    private String state;
    private String zip;
    private String country;
    private String favourite;
    private String language;
    private String msg = "Please input password!";
    private static final String CHANGE_FORM = "/WEB-INF/jsp/account/ChangeAccount.jsp";
    private static final String MAIN_FORM = "/WEB-INF/jsp/catalog/main.jsp";
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.username= req.getParameter("Username");
        this.password= req.getParameter("Password");
        this.firstname= req.getParameter("FirstName");
        this.lastname= req.getParameter("LastName");
        this.email= req.getParameter("Email");
        this.phone= req.getParameter("Phone");
        this.address1= req.getParameter("Address1");
        this.address2=req.getParameter("Address2");
        this.city= req.getParameter("City");
        this.state= req.getParameter("State");
        this.zip= req.getParameter("Zip");
        this.country= req.getParameter("Country");
        this.favourite= req.getParameter("favourite");
        this.language= req.getParameter("language");

        Account changeAccount = new Account();
        changeAccount.setUsername(this.username);
        changeAccount.setPassword(this.password);
        changeAccount.setFirstName(this.firstname);
        changeAccount.setLastName(this.lastname);
        changeAccount.setEmail(this.email);
        changeAccount.setPhone(this.phone);
        changeAccount.setAddress1(this.address1);
        changeAccount.setAddress2(this.address2);
        changeAccount.setCity(this.city);
        changeAccount.setState(this.state);
        changeAccount.setZip(this.zip);
        changeAccount.setFavouriteCategoryId(this.favourite);
        changeAccount.setLanguagePreference(this.language);
        changeAccount.setCountry(this.country);
        changeAccount.setBannerOption(true);
        changeAccount.setListOption(true);
        changeAccount.setStatus("OK");


        if(this.password == null|| this.password.equals(""))
        {
            req.setAttribute("signOnMsg1",this.msg);
            req.getRequestDispatcher(CHANGE_FORM).forward(req,resp);
        }
        else {
            AccountService accountService = new AccountService();
            accountService.changeUser(changeAccount);
            req.getSession().invalidate();
            req.getRequestDispatcher(MAIN_FORM).forward(req,resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
